/**
 * File MASSyndicationBehaviour.java created by Pablo Díaz Fernández
 * on 28/10/2011 for the project supply-chain-simulator
 */
package org.lsk.pfc.scsim.model.behaviour;

import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

import org.apache.log4j.Logger;
import org.lsk.pfc.scsim.model.agent.ControlAgent;
import org.lsk.pfc.scsim.model.messages.MessageFactory;

/**
 * Behaviour used in agents in order to syndicate to the timer of the system.
 * Sends syndication request and waits for the confirmation.
 * 
 * @author Pablo Díaz Fernández
 */
public final class MASSyndicationBehaviour extends OneShotBehaviour {

	/**
	 * Serial version ID.
	 */
	private static final long serialVersionUID = 1200067264660431537L;
	
	/**
	 * Logger for the class.
	 */
	private static final Logger LOGGER = Logger
	        .getLogger(MASSyndicationBehaviour.class);
	
	/**
	 * Execution method of the behaviour. Sends syndication request and waits
	 * for the confirmation.
	 * 
	 * @see jade.core.behaviours.Behaviour#action()
	 */
	@Override
	public void action() {
		ControlAgent agent = (ControlAgent) this.myAgent;
		ACLMessage syndicartionMsg = MessageFactory.getSyndMsg(agent.getName());

		agent.send(syndicartionMsg);

		LOGGER.debug("[" + agent.getName() + "] Synd req sent");

		//Blocks behaviour execution until a syndication acknowledge message
		//is received.
		agent.blockingReceive(MessageFactory.SYND_ACK_MSG_TEMPLATE);

		LOGGER.debug("[" + agent.getName() + "] Synd ack received");

		switch (agent.getRole()) {
			case ControlAgent.CUSTOMER:
				agent.addBehaviour(new CustomerControlAgentCB());
				break;
			case ControlAgent.BASIC:
				agent.addBehaviour(new BasicControlAgentCB());
				break;
			case ControlAgent.MANUFACTURER:
				agent.addBehaviour(new ManufacturerControlAgentCB());
				break;
			default:
				break;
		}
	}

}
